package pl.edu.agh.jemo.gui.panels.mutation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.operator.mutation.impl.BalancedMutation;

/**
 * Configuration panel allowing to adjust balanced mutation parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing mutation instance, using reference passed in constructor.
 * It also extends classic mutation panel, so it contains all components used by classic mutation.
 * 
 * @author Marcin Zbijowski
 *
 */
public class BalancedMutationConfigurationPanel extends
		ClassicMutationConfigurationPanel {

	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = -31891214892608757L;
	
	/**
	 * Reference to balanced mutation instance configured by panel.
	 */
	private BalancedMutation mutation;
	/**
	 * Field allowing to modify chance of significant mutation.
	 */
	protected JTextField strongChanceEdit;
	
	/**
	 * Creates Panel instance associated with given mutation reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param mutationReference reference to balanced mutation, that needs configuration. 
	 */
	public BalancedMutationConfigurationPanel(BalancedMutation mutationReference) {
		super(mutationReference);
		mutation = mutationReference;
		
		JLabel strongLabel = new JLabel("Significant mutation chance:");
		strongLabel.setBounds(10, 145, 150, 15);
		strongChanceEdit = new JTextField(String.valueOf(mutation.getStrongMutationChance()));
		strongChanceEdit.setBounds(10, 165, 150, 20);
		strongChanceEdit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					mutation.setStrongMutationChance(Double.parseDouble(strongChanceEdit.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong double value");
				}
			}
		});
		
		add(strongLabel);
		add(strongChanceEdit);
	}

}
